import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/Login'
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Login')
// import Home from '../components/Home'
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Home')
// import Welcome from '../components/Welcome'
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Welcome')


// import User from '../components/users/Users'
const User = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/users/Users')
// import Role from '../components/rights/Roles'
const Role = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/rights/Roles')
// import Rights from '../components/rights/Rights'
const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/rights/Rights')

// import Categories from '../components/goods/Categories'
const Categories = () => import('../components/goods/Categories')
// import Params from '../components/goods/Params'
const Params = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Params')

// import Goods from '../components/goods/Goods'
// import AddGood from '../components/goods/AddGood'
const Goods = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/Goods')
const AddGood = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/AddGood')
const EditGood = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/EditGood')


// import Orders from '../components/orders/Orders'
// import Reports from '../components/reports/Reports'
const Orders = () => import(/* webpackChunkName: "Order_Report" */ '../components/orders/Orders')
const Reports = () => import(/* webpackChunkName: "Order_Report" */ '../components/reports/Reports')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home', component: Home, redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: User },
      { path: '/roles', component: Role },
      { path: '/rights', component: Rights },
      { path: '/categories', component: Categories },
      { path: '/params', component: Params },
      { path: '/goods', component: Goods },
      { path: '/addgood', component: AddGood },
      { path: '/editgood/:id', component: EditGood, name: 'editGood' },
      { path: '/orders', component: Orders },
      { path: '/reports', component: Reports },
    ]
  }
]


const router = new VueRouter({
  routes
})

// 为路由对象，添加 beforeEach 导航守卫【需要写在router后台，否则 beforeEach 为 undefined】
/*
  to: 将要访问的路径
  from： 代表从那个路径跳转而来
  next: 是一个函数，表示放行
      next() 直接放行  next('/login') 强制跳转
 */
router.beforeEach((to, from, next) => {
  // 如果用户访问的登录页，直接放行
  if (to.path === '/login') return next()
  // 如果用户访问的后台管理页面，需要先判断是否有token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()

})

export default router
